<template>
    <div class="main-content">
        <!-- 列表页 -->
        <template v-if="showFlag">
            <el-form
                class="center-form-pv"
                :inline="true"
                :model="searchForm"
            >
              <el-form-item label="医护工号">
                <el-input v-model="searchForm.yihugonghao" placeholder="医护工号" clearable></el-input>
              </el-form-item>
              <el-form-item label="医护姓名">
                <el-input v-model="searchForm.yihuxingming" placeholder="医护姓名" clearable></el-input>
              </el-form-item>
              <el-form-item label="请假类型">
                <el-select
                    @change="qingjialeixingChange"
                    clearable
                    v-model="searchForm.qingjialeixing"
                    placeholder="请选择请假类型"
                >
                  <el-option v-for="(item, index) in qingjialeixingOptions" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="是否通过">
                <el-select @change="sfshChange" clearable v-model="searchForm.sfsh" placeholder="是否通过">
                  <el-option v-for="(item, index) in sfshOptions" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
              <el-button
                  type="primary"
                  @click="search()"
                  icon="el-icon-search"
              >查询</el-button
              >
              <el-button
                  v-if="isAuth('qingjiashenqing', '新增')"
                  type="success"
                  @click="addOrUpdateHandler()"
                  icon="el-icon-plus"
              >新增</el-button
              >
              <el-button
                  v-if="isAuth('qingjiashenqing', '删除')"
                  :disabled="dataListSelections.length <= 0"
                  type="danger"
                  icon="el-icon-delete"
                  @click="deleteHandler()"
              >删除</el-button
              >
            </el-form>

            <!-- <div> -->
            <el-table
                class="tables"
                :stripe="true"
                v-if="isAuth('qingjiashenqing', '查看')"
                :data="dataList"
                v-loading="dataListLoading"
                @selection-change="selectionChangeHandler"
            >
                <el-table-column :resizable="true" type="selection" align="center" width="50"></el-table-column>
                <el-table-column :resizable="true" :sortable="true" label="序号" type="index" width="50" />
                <el-table-column :resizable="true" :sortable="true" prop="yihugonghao" label="医护工号">
                    <template slot-scope="scope">
                        {{ scope.row.yihugonghao }}
                    </template>
                </el-table-column>
                <el-table-column :resizable="true" :sortable="true" prop="yihuxingming" label="医护姓名">
                    <template slot-scope="scope">
                        {{ scope.row.yihuxingming }}
                    </template>
                </el-table-column>
                <el-table-column :resizable="true" :sortable="true" prop="qingjialeixing" label="请假类型">
                    <template slot-scope="scope">
                        {{ scope.row.qingjialeixing }}
                    </template>
                </el-table-column>
                <el-table-column :resizable="true" :sortable="true" prop="qishishijian" label="起始时间">
                    <template slot-scope="scope">
                        {{ scope.row.qishishijian }}
                    </template>
                </el-table-column>
                <el-table-column :resizable="true" :sortable="true" prop="jieshushijian" label="结束时间">
                    <template slot-scope="scope">
                        {{ scope.row.jieshushijian }}
                    </template>
                </el-table-column>
                <el-table-column :resizable="true" :sortable="true" prop="shhf" label="审核回复"></el-table-column>
                <el-table-column :resizable="true" :sortable="true" prop="sfsh" label="审核状态">
                    <template slot-scope="scope">
                        <span v-if="scope.row.sfsh === '是'">通过</span>
                        <span v-if="scope.row.sfsh === '否'">未通过</span>
                        <span v-if="scope.row.sfsh === '待审核'">待审核</span>
                    </template>
                </el-table-column>
                <el-table-column :resizable="true" :sortable="true" v-if="isAuth('qingjiashenqing', '审核')" prop="sfsh" label="审核">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="shDialog(scope.row)">审核</el-button>
                    </template>
                </el-table-column>
                <el-table-column width="300" label="操作">
                  <template slot-scope="scope">
                    <el-button
                        v-if="isAuth('qingjiashenqing', '查看')"
                        plain
                        circle
                        icon="el-icon-view"
                        type="success"
                        size="mini"
                        @click="addOrUpdateHandler(scope.row.id, 'info')"
                    ></el-button
                    >
                    <el-button
                        v-if="isAuth('qingjiashenqing', '修改')"
                        type="primary"
                        plain
                        circle
                        icon="el-icon-edit"
                        size="mini"
                        @click="addOrUpdateHandler(scope.row.id)"
                    ></el-button
                    >
                    <el-button
                        v-if="isAuth('qingjiashenqing', '删除')"
                        type="danger"
                        plain
                        circle
                        icon="el-icon-delete"
                        size="mini"
                        @click="deleteHandler(scope.row.id)"
                    ></el-button
                    >
                  </template>
                </el-table-column>
            </el-table>
            <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                background
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageSize"
                :layout="layouts.join()"
                :total="totalPage"
                prev-text="<"
                next-text=">"
                :hide-on-single-page="true"
            ></el-pagination>
            <!-- </div> -->
        </template>

        <!-- 添加/修改页面  将父组件的search方法传递给子组件-->
        <add-or-update v-if="addOrUpdateFlag" :parent="this" ref="addOrUpdate"></add-or-update>

        <el-dialog title="审核" :visible.sync="sfshVisiable" width="50%">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="审核状态">
                    <el-select v-model="shForm.sfsh" placeholder="审核状态">
                        <el-option label="通过" value="是"></el-option>
                        <el-option label="不通过" value="否"></el-option>
                        <el-option label="待审核" value="待审核"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input type="textarea" :rows="8" v-model="shForm.shhf"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="shDialog">取 消</el-button>
                <el-button type="primary" @click="shHandler">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="批量审核" :visible.sync="sfshBatchVisiable" width="50%">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="审核状态">
                    <el-select v-model="shBatchForm.sfsh" placeholder="审核状态">
                        <el-option label="通过" value="是"></el-option>
                        <el-option label="不通过" value="否"></el-option>
                        <el-option label="待审核" value="待审核"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input type="textarea" :rows="8" v-model="shBatchForm.shhf"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="shBatchDialog">取 消</el-button>
                <el-button type="primary" @click="shBatchHandler">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
//$graphType1
//$buttonName1
//$subNameList1
import axios from 'axios';
import AddOrUpdate from './add-or-update';
export default {
    data() {
        return {
            searchForm: {
                key: '',
            },
            form: {},
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalPage: 0,
            dataListLoading: false,
            dataListSelections: [],
            showFlag: true,
            sfshVisiable: false,
            shForm: {},
            sfshBatchVisiable: false,
            shBatchForm: {
                sfsh: '',
                shhf: '',
            },
            batchIds: [],
            chartVisiable: false,
            chartVisiable1: false,
            chartVisiable2: false,
            chartVisiable3: false,
            chartVisiable4: false,
            chartVisiable5: false,
            addOrUpdateFlag: false,
            layouts: ['total', 'prev', 'pager', 'next', 'sizes', 'jumper'],
        };
    },
    created() {
        this.init();
        this.getDataList();
        this.contentStyleChange();
    },
    mounted() {},
    filters: {
        htmlfilter: function (val) {
            return val.replace(/<[^>]*>/g).replace(/undefined/g, '');
        },
    },
    components: {
        AddOrUpdate,
    },
    methods: {
        contentStyleChange() {
            this.contentPageStyleChange();
        },
        // 分页
        contentPageStyleChange() {
            let arr = [];

            // if(this.contents.pageTotal) arr.push('total')
            // if(this.contents.pageSizes) arr.push('sizes')
            // if(this.contents.pagePrevNext){
            //   arr.push('prev')
            //   if(this.contents.pagePager) arr.push('pager')
            //   arr.push('next')
            // }
            // if(this.contents.pageJumper) arr.push('jumper')
            // this.layouts = arr.join()
            // this.contents.pageEachNum = 10
        },

        init() {
            this.sfshOptions = '是,否,待审核'.split(',');
            this.qingjialeixingOptions = '事假,病假'.split(',');
        },
        search() {
            this.pageIndex = 1;
            this.getDataList();
        },

        // 获取数据列表
        getDataList() {
            this.dataListLoading = true;
            let params = {
                page: this.pageIndex,
                limit: this.pageSize,
                sort: 'id',
                order: 'desc',
            };
            if (this.searchForm.yihugonghao !== '' && this.searchForm.yihugonghao !== undefined) {
                params['yihugonghao'] = '%' + this.searchForm.yihugonghao + '%';
            }
            if (this.searchForm.sfsh !== '' && this.searchForm.sfsh !== undefined) {
                params['sfsh'] = this.searchForm.sfsh;
            }
            if (this.searchForm.yihuxingming !== '' && this.searchForm.yihuxingming !== undefined) {
                params['yihuxingming'] = '%' + this.searchForm.yihuxingming + '%';
            }
            if (this.searchForm.sfsh !== '' && this.searchForm.sfsh !== undefined) {
                params['sfsh'] = this.searchForm.sfsh;
            }
            if (this.searchForm.qingjialeixing !== '' && this.searchForm.qingjialeixing !== undefined) {
                params['qingjialeixing'] = this.searchForm.qingjialeixing;
            }
            if (this.searchForm.sfsh !== '' && this.searchForm.sfsh !== undefined) {
                params['sfsh'] = this.searchForm.sfsh;
            }
            if (this.searchForm.sfsh !== '' && this.searchForm.sfsh !== undefined) {
                params['sfsh'] = this.searchForm.sfsh;
            }
            if (this.searchForm.sfsh !== '' && this.searchForm.sfsh !== undefined) {
                params['sfsh'] = this.searchForm.sfsh;
            }
            if (this.searchForm.sfsh !== '' && this.searchForm.sfsh !== undefined) {
                params['sfsh'] = this.searchForm.sfsh;
            }
            if (this.searchForm.sfsh !== '' && this.searchForm.sfsh !== undefined) {
                params['sfsh'] = this.searchForm.sfsh;
            }
            if (this.searchForm.sfsh !== '' && this.searchForm.sfsh !== undefined) {
                params['sfsh'] = this.searchForm.sfsh;
            }
            this.$http({
                url: 'qingjiashenqing/page',
                method: 'get',
                params: params,
            }).then(({ data }) => {
                if (data && data.code === 0) {
                    this.dataList = data.data.list;
                    this.totalPage = data.data.total;
                } else {
                    this.dataList = [];
                    this.totalPage = 0;
                }
                this.dataListLoading = false;
            });
        },
        // 每页数
        sizeChangeHandle(val) {
            this.pageSize = val;
            this.pageIndex = 1;
            this.getDataList();
        },
        // 当前页
        currentChangeHandle(val) {
            this.pageIndex = val;
            this.getDataList();
        },
        // 多选
        selectionChangeHandler(val) {
            this.dataListSelections = val;
        },
        // 添加/修改
        addOrUpdateHandler(id, type) {
            this.showFlag = false;
            this.addOrUpdateFlag = true;
            this.crossAddOrUpdateFlag = false;
            if (type !== 'info') {
                type = 'else';
            }
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id, type);
            });
        },
        // 审核窗口
        shDialog(row) {
            this.sfshVisiable = !this.sfshVisiable;
            if (row) {
                this.shForm = {
                    yihugonghao: row.yihugonghao,
                    yihuxingming: row.yihuxingming,
                    qingjialeixing: row.qingjialeixing,
                    qishishijian: row.qishishijian,
                    jieshushijian: row.jieshushijian,
                    qingjiashiyou: row.qingjiashiyou,
                    sfsh: row.sfsh,
                    shhf: row.shhf,
                    id: row.id,
                };
            }
        },
        // 审核
        shHandler() {
            this.$confirm(`确定操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.$http({
                    url: 'qingjiashenqing/update',
                    method: 'post',
                    data: this.shForm,
                }).then(({ data }) => {
                    if (data && data.code === 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList();
                                this.shDialog();
                            },
                        });
                    } else {
                        this.$message.error(data.msg);
                    }
                });
            });
        },
        //批量审核窗口
        shBatchDialog() {
            this.sfshBatchVisiable = !this.sfshBatchVisiable;
            this.batchIds = this.dataListSelections.map(item => {
                return Number(item.id);
            });
        },
        //批量审核
        shBatchHandler() {
            this.$confirm(`确定一键审核操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.$http({
                    url: 'qingjiashenqing/shBatch?sfsh=' + this.shBatchForm.sfsh + '&shhf=' + this.shBatchForm.shhf,
                    method: 'post',
                    data: this.batchIds,
                }).then(({ data }) => {
                    if (data && data.code === 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList();
                                this.shBatchDialog();
                            },
                        });
                    } else {
                        this.$message.error(data.msg);
                    }
                });
            });
        },
        // 下载
        download(file) {
            window.open(`${file}`);
        },
        // 删除
        deleteHandler(id) {
            var ids = id
                ? [Number(id)]
                : this.dataListSelections.map(item => {
                      return Number(item.id);
                  });
            this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.$http({
                    url: 'qingjiashenqing/delete',
                    method: 'post',
                    data: ids,
                }).then(({ data }) => {
                    if (data && data.code === 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.search();
                            },
                        });
                    } else {
                        this.$message.error(data.msg);
                    }
                });
            });
        },
    },
};
</script>
